package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.nfb.media.nfbPlayer.NFBPyroBrew;
	import ca.turbulent.media.Pyro;
	import ca.nfb.media.nfbPlayer.ui.ToolTip;
	
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class Options extends NFBPlayerView
	{
		private var selected			:int;
		
		public function Options()
		{
			super();
		}
		
		override protected function checkLang():void
		{
			switch(ComModule.getLang())
			{
				case "EN":
				labels_mc.gotoAndStop(1);
				standardQuality_mc.gotoAndStop(1);
				lowQuality_mc.gotoAndStop(1);
				highQuality_mc.gotoAndStop(1);
				break;
				
				case "FR":
				labels_mc.gotoAndStop(2);
				standardQuality_mc.gotoAndStop(2);
				lowQuality_mc.gotoAndStop(2);
				highQuality_mc.gotoAndStop(2);
				break;
			}
		}
		
		override public function close(evt:*=null):void
		{
			this.visible = false;
			core.overlayedPanelOpen = false;
			
			if (this.previousState == NFBPlayer.PREVIEW_IMAGE_STATE)
			{
				core.setCurrentState(NFBPlayer.PREVIEW_IMAGE_STATE);
				if (core.embeddedMode)
					core.embeddedWelcomeView.show();	
					
				if (core.warningMessageView.wasShowing)
					core.warningMessageView.visible = true;	
						
				return;	
			}
			
			if (!core.wasPaused)
			{
				if (this.previousState != NFBPlayer.END_PLAYLIST_STATE)
				{
					player.play();
				}
				
			}
			else
			{
				core.controlBar.overlay_mc.mouseEnabled = false;
				core.controlBar.overlay_mc.alpha = 0;	
			}
			core.setCurrentState(NFBPlayer.DEFAULT_STATE);
		}
		
		override protected function addedToStage(evt:Event):void 
		{ 
			registerStageEvents();	
			y 	= 111;
			x 	= 7;
			this.visible = false;
			this.setProperties([lowSpeed_mc, standardSpeed_mc, highSpeed_mc], {mouseEnabled:true, buttonMode:true, useHandCursor:true}, speedButtonHandler);
			this.lockUI(true, [labels_mc]);
			MovieClip(closeButton_mc).useHandCursor = true;
			MovieClip(closeButton_mc).buttonMode = true;
			MovieClip(closeButton_mc).mouseEnabled = true;
			
			MovieClip(close_mc).useHandCursor = true;
			MovieClip(close_mc).buttonMode = true;
			MovieClip(close_mc).mouseEnabled = true;
			close_mc.addEventListener(MouseEvent.CLICK, close, false, 0, true);
			closeButton_mc.addEventListener(MouseEvent.CLICK, close, false, 0, true);
			
			ToolTip.addToolTipLabelElement("lowSpeed_mc", "Low quality", "Basse qualité");
			ToolTip.addToolTipLabelElement("standardSpeed_mc", "Standard quality", "Qualité intermédiaire");
			ToolTip.addToolTipLabelElement("highSpeed_mc", "High quality", "Haute qualité");
			
			core.screenReaderManager.addAccess(closeButton_mc, "Close", "Fermer");
			core.screenReaderManager.addAccess(lowSpeed_mc, "Low quality", "Basse qualité");
			core.screenReaderManager.addAccess(standardSpeed_mc, "Standard quality", "Qualité intermédiaire");
			core.screenReaderManager.addAccess(highSpeed_mc, "High quality", "Haute qualité");
			
			resetButtons();
			player.addEventListener(NFBPyroBrew.STREAM_QUALITY_CHANGE, setCorrectSpeedLabels, false, 0, true);
			
			checkSpeed();
		}
		
		private function resetButtons():void
		{
			MovieClip(labels_mc).tabChildren = false;
			labels_mc.gotoAndStop(1);
			lowSpeed_mc.gotoAndStop(1);
			standardSpeed_mc.gotoAndStop(1);
			highSpeed_mc.gotoAndStop(1);	 
		}
		
		private function checkSpeed():void
		{
			lowQuality_mc.visible = false;
			standardQuality_mc.visible = false;
			highQuality_mc.visible = false;
			
			lowSpeed_mc.mouseEnabled = true;
			standardSpeed_mc.mouseEnabled = true;
			highSpeed_mc.mouseEnabled = true;
			
			switch(player.streamQuality)
			{
				case Pyro.CONNECTION_SPEED_LOW:
				case "LOW":
				lowSpeed_mc.gotoAndStop(2);
				lowQuality_mc.visible = true;
				lowSpeed_mc.mouseEnabled = false;	
				break;
				
				case Pyro.CONNECTION_SPEED_MEDIUM:
				case "STANDARD":
				case "NORMAL":
				default:
				standardSpeed_mc.gotoAndStop(2);
				standardQuality_mc.visible = true;
				standardSpeed_mc.mouseEnabled = false;
				break;
				
				case Pyro.CONNECTION_SPEED_HIGH:
				case "HIGH":
				case "EXTREME":
				highSpeed_mc.gotoAndStop(2);
				highQuality_mc.visible = true;
				highSpeed_mc.mouseEnabled = false;
				break;	
			}
			
			
			trace("has xspeed"+ComModule.getInstance().latestMovieHasExtremeSpeed);
			
			if (!ComModule.getInstance().latestMovieHasLowSpeed)
			{	 
				MovieClip(lowSpeed_mc).mouseEnabled = false;
				MovieClip(lowSpeed_mc).mouseChildren = false;
				MovieClip(lowSpeed_mc).useHandCursor = false;
				MovieClip(lowSpeed_mc).buttonMode = false;
				MovieClip(lowSpeed_mc).alpha = .3;
			}
			else
			{
				MovieClip(lowSpeed_mc).mouseEnabled = true;
				MovieClip(lowSpeed_mc).mouseChildren = true;
				MovieClip(lowSpeed_mc).useHandCursor = true;
				MovieClip(lowSpeed_mc).buttonMode = true;
				MovieClip(lowSpeed_mc).alpha = 1;
			}
			
			if (!ComModule.getInstance().latestMovieHasNormalSpeed)
			{	 
				MovieClip(standardSpeed_mc).mouseEnabled = false;
				MovieClip(standardSpeed_mc).mouseChildren = false;
				MovieClip(standardSpeed_mc).useHandCursor = false;
				MovieClip(standardSpeed_mc).buttonMode = false;
				MovieClip(standardSpeed_mc).alpha = .3;
			}
			else
			{
				MovieClip(standardSpeed_mc).mouseEnabled = true;
				MovieClip(standardSpeed_mc).mouseChildren = true;
				MovieClip(standardSpeed_mc).useHandCursor = true;
				MovieClip(standardSpeed_mc).buttonMode = true;
				
				MovieClip(standardSpeed_mc).alpha = 1;
			}
			
			if (!ComModule.getInstance().latestMovieHasExtremeSpeed)
			{	 
				MovieClip(highSpeed_mc).mouseEnabled = false;
				MovieClip(highSpeed_mc).mouseChildren = false;
				MovieClip(highSpeed_mc).useHandCursor = false;
				MovieClip(highSpeed_mc).buttonMode = false;
				MovieClip(highSpeed_mc).alpha = .3;
			}	
			else
			{
				 
				MovieClip(highSpeed_mc).mouseEnabled = true;
				MovieClip(highSpeed_mc).mouseChildren = true;
				MovieClip(highSpeed_mc).useHandCursor = true;
				MovieClip(highSpeed_mc).buttonMode = true;
				MovieClip(highSpeed_mc).alpha = 1;
			}	
		}	
			
		private function speedButtonHandler(evt:MouseEvent):void
		{
			switch(String(evt.type).toLowerCase())
			{	
				case "click":
				this.resetButtons();
				close();
				
				switch(evt.currentTarget)
				{
					case lowSpeed_mc:
					lowSpeed_mc.gotoAndStop(2);
					ComModule.getInstance().setStats("streamConnectionSpeedChanged :LOW");
					player.setStreamQuality(Pyro.CONNECTION_SPEED_LOW);
					break;
					
					case standardSpeed_mc:
					standardSpeed_mc.gotoAndStop(2);
					ComModule.getInstance().setStats("streamConnectionSpeedChanged :NORMAL");
					player.setStreamQuality(Pyro.CONNECTION_SPEED_MEDIUM);
					break;
					
					case highSpeed_mc:
					highSpeed_mc.gotoAndStop(2);
					ComModule.getInstance().setStats("streamConnectionSpeedChanged :EXTREME");
					player.setStreamQuality(Pyro.CONNECTION_SPEED_HIGH);
					break;
				}
				checkSpeed();
				break;
				
				case "mouseover":
				if (MovieClip(evt.currentTarget).currentFrame == 1)
					MovieClip(evt.currentTarget).gotoAndStop(3);
				
				switch(evt.currentTarget)
				{
					case lowSpeed_mc:
					core.toolTip.showToolTip(ToolTip.getLabel('lowSpeed_mc'), lowSpeed_mc);
					break;
					
					case standardSpeed_mc:
					core.toolTip.showToolTip(ToolTip.getLabel('standardSpeed_mc'), standardSpeed_mc);
					break;
					
					case highSpeed_mc:
					core.toolTip.showToolTip(ToolTip.getLabel('highSpeed_mc'), highSpeed_mc);
					break;
				}	
				break;
				
				case "mouseout":
				if (MovieClip(evt.currentTarget).currentFrame == 3)
					MovieClip(evt.currentTarget).gotoAndStop(1);
				
				core.toolTip.hideToolTip();	
				break;	
			}
		}
		
		private function setCorrectSpeedLabels(evt:Event=null):void
		{
			this.resetButtons();
			switch(player.streamQuality)
			{
				case "LOW":
				case Pyro.CONNECTION_SPEED_LOW:
				lowSpeed_mc.gotoAndStop(2);
				break;
				
				case Pyro.CONNECTION_SPEED_MEDIUM:
				case "STANDARD":
				case "NORMAL":
				standardSpeed_mc.gotoAndStop(2);
				break;
				
				case Pyro.CONNECTION_SPEED_HIGH:
				case "HIGH":
				case "EXTREME":
				highSpeed_mc.gotoAndStop(2);
				break;
			}
			checkSpeed();
		}
		
		override protected function coreStateUpdated(evt:Event=null):void 
		{ 
			if (core.currentState == NFBPlayer.OPTIONS_STATE)
			{
				core.overlayedPanelOpen = true;
				checkSpeed();
				this.visible = true;
				
				player.tabIndex = -1;
				core.controlBar.playPauseButton_mc.tabIndex = -1;
			}
			else
			{
				if (core.currentState != NFBPlayer.SHARING_STATE && core.currentState != NFBPlayer.MORE_INFOS_STATE)
				{
					//	core.overlayedPanelOpen = false;
				}
				
				this.visible = false;
			}
		}
	}
}